<?php
function fnmatch2($pattern, $string) {
	for ($op = 0, $npattern = '', $n = 0, $l = strlen($pattern); $n < $l; $n++) {
		switch ($c = $pattern[$n]) {
			case '\\':
				$npattern .= '\\' . @$pattern[++$n];
				break;
			case '.': case '+': case '^': case '$': case '(': case ')': case '{': case '}': case '=': case '!': case '<': case '>': case '|':
				$npattern .= '\\' . $c;
				break;
			case '?': case '*':
				$npattern .= '.' . $c;
				break;
			case '[': case ']': default:
				$npattern .= $c;
				if ($c == '[') {
					$op++;
				} else if ($c == ']') {
					if ($op == 0) return false;
					$op--;
				}
				break;
		}
	}

	if ($op != 0) return false;

	return preg_match('/' . $npattern . '/i', $string);
}

$query = $_GET['q'];
$lines = file('names2.txt');

echo "<div>";
//echo "The query was: ".$query."<br />";

// Loop through our array, print the image names in div tags
foreach ($lines as $line) {
	$trimmed = trim($line, "\r\n");
	if(fnmatch2($query, $line)) {
		echo "<div id=\"{$trimmed}\" onclick=\"javascript:plot('{$trimmed}')\">{$trimmed}</div>";
	}
	
}
echo "</div>";

?>